package com.wj.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wj.crm.base.BaseService;
import com.wj.crm.dao.DataDicMapper;
import com.wj.crm.exceptions.ParamsException;
import com.wj.crm.query.DataDicQuery;
import com.wj.crm.query.UserQuery;
import com.wj.crm.utils.AssertUtil;
import com.wj.crm.utils.Md5Util;
import com.wj.crm.vo.DataDic;
import com.wj.crm.vo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WangJin
 * @version 1.0
 * @date 2022/2/9 21:45
 */
@Service
public class DataDicService extends BaseService<DataDic,Integer> {

    @Autowired
    private DataDicMapper dataDicMapper;

    /**
     * 多条件分页查询数据字典 (BaseService 中有对应的方法)
     * @param query
     * @return
     */
    public Map<String, Object> selectByParams (DataDicQuery query) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<DataDic> pageInfo = new PageInfo<DataDic>(dataDicMapper.selectByParams(query));
        map.put("code",0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 添加数据字典
     * @param dataDic
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addDic(DataDic dataDic){
        //1.参数校验
        checkDataDicParams(dataDic.getDataDicName(),dataDic.getDataDicValue(),dataDic.getCode(),null);

        //2.设置参数的默认值
        dataDic.setCreateDate(new Date());
        dataDic.setUpdateDate(new Date());
        dataDic.setIsValid(1);
        //3.执行添加操作，判断影响行数
        AssertUtil.isTrue(dataDicMapper.insertSelective(dataDic) != 1,"添加字典数据失败！");

    }

    /**
     * 修改数据字典
     * @param dataDic
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDic(DataDic dataDic){
        //判断字典ID是否为空，且数据存在
        AssertUtil.isTrue(null == dataDic.getId(),"待更新记录不存在！");
        //通过id查询数据
        dataDicMapper.selectByPrimaryKey(dataDic.getId());
        //1.参数校验
        checkDataDicParams(dataDic.getDataDicName(),dataDic.getDataDicValue(),dataDic.getCode(), dataDic.getId());
        //设置默认值
        dataDic.setUpdateDate(new Date());
        //3.执行更新操作，判断影响行数
        AssertUtil.isTrue(dataDicMapper.updateByPrimaryKeySelective(dataDic) != 1,"添加用户失败！");

    }

    /**
     * 数据字典删除
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteByIds(Integer[] ids) {
        //判断ids是否为空，长度是否大于0
        AssertUtil.isTrue(ids == null || ids.length == 0,"待删除记录不存在！");
        //执行删除操作，判断受影响行数
        AssertUtil.isTrue(dataDicMapper.deleteBatch(ids) != ids.length,"数据字典删除失败！");

        //遍历数据字典Id的数组
        for(Integer dicId : ids){
            //通过字典Id查询对应的数据字典
            Integer count = dataDicMapper.countDicByUserId(dicId);
            //判断字典数据记录是否存在
            if(count > 0){
                //通过用户id删除对应的数据字典
                AssertUtil.isTrue(dataDicMapper.deleteDicRoleByUserId(dicId) != count,"删除字典数据失败！");
            }

        }
    }

    public void checkDataDicParams(String dicName,String dicValue,String code,Integer dicId){
        //判断用户是否为空
        AssertUtil.isTrue(StringUtils.isBlank(dicName),"用户名不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(dicValue),"用户名不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(code),"用户名不能为空！");
        //判断用户名的唯一性
        //通过用户查询用户对象
        List<DataDic> temp = dataDicMapper.queryDicByDicValue(dicValue);
            if (temp.size() >= 1){
                throw  new ParamsException("字典值已存在，请重新输入！");
            }


    }

}
